﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OsuTool
{
    public partial class Frm_Main : Form
    {
        private readonly string _defaultKeyPosition = "1123344";
        private readonly string _type1KeyPosition = "1 1 2 3 3 4 4";

        public Frm_Main()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void btn_choose_file_Click(object sender, EventArgs e)
        {
            ofd.ShowDialog();
            var fileList = ofd.FileNames;
            foreach (var fileName in fileList)
            {
                if (list_files.Items.Contains(fileName)) continue;
                list_files.Items.Add(fileName);
            }
        }

        private void cbx_aim_keys_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_convert_mode.MaxLength = Convert.ToInt32(cbx_aim_key_count.SelectedItem.ToString());
        }

        private void btn_invoke_Click(object sender, EventArgs e)
        {
            try
            {
                if (list_files.Items.Count == 0)
                {
                    throw new Exception("请选择文件！");
                }

                var keyCountStr = cbx_key_count.Text;
                var aimKeyCountStr = cbx_aim_key_count.Text;
                var convertMode = txt_convert_mode.Text;
                if (keyCountStr.Length == 0 || aimKeyCountStr.Length == 0 || convertMode.Length == 0)
                {
                    throw new Exception("当前键数、目标键数、转换模式不能为空！");
                }

                var aimKeyCount = Convert.ToInt32(aimKeyCountStr);
                var keyCount = Convert.ToInt32(keyCountStr);

                if (convertMode.Length != aimKeyCount)
                {
                    throw new Exception("转换模式字符串的长度应与目标键数相同！");
                }

                var mapConverter = new KeyPositionConverter(keyCount, aimKeyCount, convertMode);

                new Thread(new ThreadStart(delegate
                {
                    Log("开始执行任务，请等待");
                    foreach (string file in list_files.Items)
                    {
                        try
                        {
                            mapConverter.ConvertKepPosition(file);
                            Log($"<{file}>转谱成功");
                        }
                        catch (Exception ex)
                        {
                            Log($@"<{file}>转谱失败——{ex.Message}");
                        }
                    }

                    Log("任务完成");
                })).Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            cbx_aim_key_count.SelectedIndex = 3;
            cbx_key_count.SelectedIndex = 0;
            rb_default.Checked = true;
            rb_ln_ad_2.Checked = true;
            rb_ln_rm_1.Checked = true;
        }

        private void list_menu_delete_Click(object sender, EventArgs e)
        {
            list_files.Items.RemoveAt(list_files.SelectedIndex);
        }

        private void list_menu_delete_all_Click(object sender, EventArgs e)
        {
            list_files.Items.Clear();
        }

        private void list_menu_add_Click(object sender, EventArgs e)
        {
            btn_choose_file_Click(sender, e);
        }

        private void Log(string text)
        {
            rich_log.AppendText($"[{DateTime.Now:hh:mm:ss}]{text}\n");
            rich_log.ScrollToCaret();
        }

        private void btn_ln_rm_Click(object sender, EventArgs e)
        {
            try
            {
                var mapConverter = new RemoveLNConverter();
                if (list_files.Items.Count == 0)
                {
                    throw new Exception("请选择文件");
                }

                new Thread(new ThreadStart(delegate
                {
                    Log("开始执行任务，请等待");
                    foreach (string file in list_files.Items)
                    {
                        try
                        {
                            RemoveLn(mapConverter, file);
                            Log($"<{file}>转谱成功");
                        }
                        catch (Exception ex)
                        {
                            Log($@"<{file}>转谱失败——{ex.Message}");
                        }
                    }

                    Log("任务完成");
                })).Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RemoveLn(RemoveLNConverter converter, string file)
        {
            if (rb_ln_rm_1.Checked)
            {
                converter.ConvertNoLn(file);
            }
            else if (rb_ln_rm_2.Checked)
            {

            }
        }

        private void btn_clear_all_Click(object sender, EventArgs e)
        {
            list_files.Items.Clear();
        }

        private void rb_default_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_default.Checked)
            {
                txt_convert_mode.Text = _defaultKeyPosition;
            }
        }

        private void rb_type1_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_type1.Checked)
            {
                txt_convert_mode.Text = _type1KeyPosition;
            }
        }

        private void btn_ln_add_Click(object sender, EventArgs e)
        {

        }
    }
}